package data_structure.stack;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * @Author: lijun
 * @Date: 2018/11/1 11:31
 */
public class InorderTraversal {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
        while (node != null || !stack.isEmpty()) {
            if (node != null) {
                stack.push(node);   //直接压栈
                node = node.left;
            } else {
                node = stack.pop(); //出栈并访问
                list.add(node.val);
                node = node.right;
            }
        }
        return list;
    }

}
